package com.sdkx.app.ViewModel;

import android.text.TextUtils;

import com.sdkx.app.R;
import com.sdkx.app.model.DistanceObject;
import com.sdkx.app.model.Mark;
import com.sdkx.app.util.CharacterParser;
import com.sdkx.app.util.PinyinComparator;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 标记管理类
 */
public class MarkManager {
    /**
     * 汉字转换成拼音的类
     */
    private static CharacterParser characterParser;
    private static  ArrayList<MarkShape> shapes;
    private static PinyinComparator pinyinComparator=null;

    /**
     * 获取所有标记
     *
     * @return
     */
    public static List<Mark> getItems(String name) {
        if(name==null)
            return LitePal.findAll(Mark.class);
        else
            return LitePal.where("name like ?", "%"+name+"%").find(Mark.class);
    }

    /**
     * 返回指定类型的标记
     * @param type
     * @return
     */
    public static List<Mark> getItemsByType(int type){
        return LitePal.where("type = ?",String.valueOf(type)).find(Mark.class);
    }

    /**
     * 获取距离指定地点距离最近的几个点
     *
     * @param lon   经度
     * @param lat   纬度
     * @param limit 个数
     * @param type  类型，-1代表获取所有类型的点
     * @return
     */
    public static List<Mark> getItemsByDistance(double lon, double lat, int limit, int type) {
        List<Mark> allMarks;
        if (type == -1)
            allMarks = getItems(null);
        else
            allMarks = getItemsByType(type);
        List<DistanceObject> tempObjects = new ArrayList<>();
        for (Mark mark : allMarks
                ) {
            //TODO：数据库接口更改，需要重新编写代码逻辑
//            DistanceObject tempObject = new DistanceObject(mark.getId(), SearchHelper.distanceOfLatLong(lon, lat, mark.getLongitude(), mark.getLatitude()));
//            tempObjects.add(tempObject);
        }
        Collections.sort(tempObjects);
        if (limit > tempObjects.size()) {
            limit = tempObjects.size();
        }
        tempObjects = tempObjects.subList(0, limit);
        List<Mark> newList = new ArrayList<>();
        for (DistanceObject distanceObject : tempObjects
                ) {
            for (Mark mark : allMarks
                    ) {
                if (mark.getId() == distanceObject.getId()) {
                    newList.add(mark);
                    break;
                }
            }
        }
        return newList;
    }

    public static Mark getItem(int id){
        List<Mark> wayPoints = LitePal.where("id = ?", String.valueOf(id)).find(Mark.class);
        if(wayPoints.size()>0)
            return wayPoints.get(0);
        else
            return null;
    }
    /**
     * 查找指定标记
     * @param name
     * @return
     */
    public static Mark getItem(String name) {
        List<Mark> marks = LitePal.where("name = ?", name).find(Mark.class);
        if (marks.size() > 0)
            return marks.get(0);
        else
            return null;
    }

    /**
     * 添加标记
     * @param mark
     * @return
     */
    public static boolean insert(Mark mark) {
        if(mark ==null)
            return false;
        return mark.save();
    }

    /**
     * 批量存储，大量数据存储时采用此方法速度较快
     * @param marks
     */
    public static void insertList(List<Mark> marks){
        if(marks==null)
            return;
        LitePal.saveAll(marks);
    }

    /**
     * 判断一个名称是否已经使用
     * @param name
     * @return
     */
    public static boolean contains(String name){
        return LitePal.where("name = ?",name).count(Mark.class)>0;
    }

    /**
     * 删除标记
     * @param name
     */
    public static void delete(String name){
        LitePal.deleteAll(Mark.class,"name = ?",name);
    }

    /**
     * 删除所有标记
     */
    public static void deleteAll(){
        LitePal.deleteAll(Mark.class);
    }

    /**
     * 更新标记，此标记对象必须是已经持久化的对象
     * @param mark
     */
    public static int  update(Mark mark){
        if(mark==null||!mark.isSaved()){
             return -1;
        }
       return  mark.update(mark.getId());
    }
    /**
     * 根据航点输入框中的值来过滤数据并更新ListView
     *
     * @param filterStr
     */
    public static List<Mark> filterData(String filterStr, List<Mark> marklist) {
        List<Mark> filterDateList = new ArrayList<Mark>();
        if (characterParser == null) characterParser = CharacterParser.getInstance();
        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = marklist;
        } else {
            filterDateList.clear();
            String name, pyname, sxname;//名称，拼音，拼音缩写
            filterStr = filterStr.toUpperCase();
            for (Mark sortModel : marklist) {
                if(sortModel.getName()==null) continue;
                name = sortModel.getName().toUpperCase();
                pyname = characterParser.getSelling(name).toUpperCase();
                sxname = characterParser.getSellingSX(name).toUpperCase();
                if (-1 != (name.indexOf(filterStr))
                        || pyname.startsWith(filterStr)
                        || -1 != (sxname.indexOf(filterStr))
                        ) {
                    filterDateList.add(sortModel);
                }

            }
        }
        if(filterDateList.size()>1) {
//            // 根据a-z进行排序
//            Collections.sort(filterDateList, new PinyinComparator());
        }
        return filterDateList;
    }
    public  static List<Mark>  sort( List<Mark> list){
        if (pinyinComparator == null) pinyinComparator = new PinyinComparator();
        if(list.size()>1) {
            Collections.sort(list, pinyinComparator);
        }
        return list;
    }
    public static class MarkShape{
        public String shapName;
        public int drawableId;
        public MarkShape(String name,int id){
            shapName = name;
            drawableId = id;
        }
    }

    public static  ArrayList<MarkShape> getAllMarkShape(){
        if(shapes == null) {
            shapes = new ArrayList<MarkShape>();
            shapes.add(new MarkShape("图形1", R.drawable.mark_type1));
            shapes.add(new MarkShape("图形2", R.drawable.mark_type2));
            shapes.add(new MarkShape("图形3", R.drawable.mark_type3));
            shapes.add(new MarkShape("图形4", R.drawable.mark_type4));
        }
        return shapes;
    }
    private class MarkComparator implements Comparator<Mark>{

        @Override
        public int compare(Mark o1, Mark o2) {

            return 0;
        }
    }
}
